package com.smedi.ismedi.progress.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.api.dto.request.ProcessTaskListRequest;
import com.smedi.ismedi.progress.core.application.command.ProcessInstanceStartCommand;
import com.smedi.ismedi.progress.core.application.command.ProcessListQuery;
import com.smedi.ismedi.progress.core.application.command.ProcessTaskCancelCommand;
import com.smedi.ismedi.progress.core.application.command.ProcessTaskCompleteCommand;
import com.smedi.ismedi.progress.core.domain.aggregatemodel.ProcessTask;
import com.smedi.ismedi.progress.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.progress.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.progress.core.port.adapter.service.ProcessRemoteService;
import com.smedi.ismedi.inmail.api.dto.request.ProcessInstanceStartRequest;
import com.smedi.ismedi.inmail.api.dto.request.ProcessTaskCancelRequest;
import com.smedi.ismedi.inmail.api.dto.request.ProcessTaskCompleteRequest;
import com.smedi.ismedi.inmail.api.dto.response.ProcessTaskResponse;
import com.smedi.ismedi.inmail.api.feign.RemoteProcessFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ProcessRemoteServiceImpl implements ProcessRemoteService {

    private final RemoteProcessFeignApi remoteProcessFeignApi;

    private final AdapterConverter adapterConverter;

    @Override
    public void startProcessInstance(ProcessInstanceStartCommand processInstanceStartCommand) {
        ProcessInstanceStartRequest processInstanceStartRequest = adapterConverter.processInstanceStartCommandToProcessInstanceStartRequest(processInstanceStartCommand);
        ResponseData<ProcessTaskResponse> startProcessInstance =  remoteProcessFeignApi.startProcessInstance(processInstanceStartRequest);
        if (!startProcessInstance.isSuccess()){
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, startProcessInstance.getApplication(), String.valueOf(startProcessInstance.getErrorMsg()));
        }
    }

    @Override
    public ProcessTask completeProcessTask(ProcessTaskCompleteCommand processTaskCompleteCommand) {
        ProcessTaskCompleteRequest processTaskCompleteRequest = adapterConverter.processTaskCompleteCommandToProcessTaskCompleteRequest(processTaskCompleteCommand);
//        ProcessTaskResponse processTaskResponse = remoteProcessFeignApi.completeProcessTask(processTaskCompleteRequest).getData();
        ResponseData<ProcessTaskResponse> completeProcessTask = remoteProcessFeignApi.completeProcessTask(processTaskCompleteRequest);
        if (!completeProcessTask.isSuccess()){
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, completeProcessTask.getApplication(), String.valueOf(completeProcessTask.getErrorMsg()));
        }
        return adapterConverter.processTaskResponseToProcessTask(completeProcessTask.getData());
    }

    @Override
    public ProcessTask cancelProcessTask(ProcessTaskCancelCommand processTaskCancelCommand) {
        ProcessTaskCancelRequest processTaskCancelRequest = adapterConverter.processTaskCancelCommandToProcessTaskCancelRequest(processTaskCancelCommand);
//        ProcessTaskResponse processTaskResponse = remoteProcessFeignApi.cancelProcessTask(processTaskCancelRequest).getData();
        ResponseData<ProcessTaskResponse> cancelProcessTask = remoteProcessFeignApi.cancelProcessTask(processTaskCancelRequest);
        if (!cancelProcessTask.isSuccess()){
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, cancelProcessTask.getApplication(), String.valueOf(cancelProcessTask.getErrorMsg()));
        }
        return adapterConverter.processTaskResponseToProcessTask(cancelProcessTask.getData());
    }

    @Override
    public List<ProcessTask> getProcessList(ProcessListQuery processListQuery) {
        ProcessTaskListRequest processTaskListRequest = adapterConverter.processListQueryToProcessTaskListRequest(processListQuery);
        ResponseData<List<ProcessTaskResponse>> data = remoteProcessFeignApi.getProcessTaskList(processTaskListRequest);
        if (!data.isSuccess()){
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, data.getApplication(), String.valueOf(data.getErrorMsg()));
        }
        List<ProcessTaskResponse> processTaskList = data.getData();
        return adapterConverter.processTaskResponseListToProcessTaskList(processTaskList);
    }
}
